<script setup lang="ts">
import {useRouter} from "vue-router";
import {ref} from "vue";
import myAxios from "../plugins/myAxios";
import {showFailToast, showSuccessToast} from "vant";

const router = useRouter();

// 表单数据
const userType = ref({
  userAccount:'',
  userPassword:'',
  checkPassword:'',
  planetCode:'',
});

//  提交
const onSubmit = async () => {
  console.log("what this is",userType.value)
  await myAxios.post("/user/register", userType.value).then(res => {
    if (res?.code === 0 && res.data) {
      showSuccessToast('添加成功');
      router.push({
        path: '/user/login',
      });
    } else {
      showFailToast('添加失败');
    }
  })
}
</script>

<template>
  <div class="register-container">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
            v-model="userType.userAccount"
            name="用户名"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
            v-model="userType.userPassword"
            type="password"
            name="密码"
            label="密码"
            placeholder="请输入密码"
        />
        <van-field
            v-model="userType.checkPassword"
            type="password"
            name="第二次密码"
            label="第二次密码"
            placeholder="请再次输入密码"
        />
        <van-field
            v-model="userType.planetCode"
            name="id"
            label="id"
            placeholder="请输入id"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          注册
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style scoped>

</style>